<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <div style="width:100%;float:left">
              <table style="width:100%;font-size:12px" border="0" class="table1style">
                  <col style="width:5%;" />
                  <col style="width:15%" />
                  <col style="width:5%" />
                  <col style="width:15%" />
                  <col style="width:5%" />
                  <col style="width:15%" />
                  <col style="width:5%" />
                  <col style="width:11%" />
                  <col style="width:5%" />
                  <col style="width:19%" />
                  <tr>
                      <td align="right">标本日期</td>
                      <td>
                          <el-date-picker v-model="txtrptDatetime" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']" size="mini" :clearable="false" style="width:210px"></el-date-picker>
                      </td>
                      <td align="right">申请科室</td>
                      <td>
                          <table>
                              <tr>
                                  <td>
                                      <rm-ddl-common datatype="dept" dataname="科室" v-model="txtdeptno" width="130px" @change="deptnoChange"></rm-ddl-common>
                                  </td>
                                  <td>
                                      <el-button @click.native="multiFun('dept')" style="background-color:#e6e6e6;color:black">多选</el-button>
                                  </td>
                              </tr>
                          </table>
                      </td>
                      <td align="right">申请病区</td>
                      <td>
                          <rm-ddl-common :appendtobody="true" datatype="ward" dataname="病区" v-model="txtwardno" width="100%" @change="wardnoChange"></rm-ddl-common>
                      </td>
                      <td align="right">打印状态</td>
                      <td>
                          <el-select v-model="txtprintstat" placeholder="">
                              <el-option v-for="(item,index) in printOptions" :key="index" :value="item.value" :label="item.label"></el-option>
                          </el-select>
                      </td>
                      <td align="right">送检医院</td>
                      <td>
                          <rm-ddl-common datatype="hospital" dataname="医院" @change="hospcodeChange" v-model="txthospcode" width="100%"></rm-ddl-common>
                      </td>
                  </tr>
                  <tr>
                      <td align="right">报告单元</td>
                      <td>
                          <rm-ddl-rptunit prgno="LIS331" v-model="txtrptunit" filterable @change="handlerrptitemchange" :appendtobody="true" visible-change:true width="210px" :clearable="false" :multiple_choice="true" ref="rptunitinput"></rm-ddl-rptunit>
                      </td>
                      <td align="right">病人来源</td>
                      <td>
                          <table>
                              <tr>
                                  <td>
                                      <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人来源" v-model="txtpattypecode" width="130px" @change="pattypeChange"></rm-ddl-common>
                                  </td>
                                  <td>
                                      <el-button @click.native="multiFun('pat')" style="background-color:#e6e6e6;color:black">多选</el-button>
                                  </td>
                              </tr>
                          </table>
                      </td>
                      <td align="right">科室组别</td>
                      <td>
                          <rm-ddl-common datatype="deptcomb" dataname="科室组" v-model="txtdeptgroup" width="100%" @change="deptgroupChange"></rm-ddl-common>
                      </td>
                      <td align="right">审核状态</td>
                      <td>
                          <el-select v-model="txtchkstate" placeholder="" style="width:100%">
                              <el-option v-for="(item,index) in chkOptions" :key="index" :value="item.value" :label="item.label"></el-option>
                          </el-select>
                      </td>
                      <td align="right">查询模版</td>
                      <td>
                          <table style="width:100%">
                              <tr>
                                  <td style="width:100%">
                                      <el-select style="width:100%" v-model="txtmodle" @change="modChange($event)" clearable filterable @clear="ClearMod">
                                          <el-option v-for="(item,index) in modelOptons" :value="item.value" :label="item.label" :key="index"></el-option>
                                      </el-select>
                                  </td>
                                  <td>
                                      <el-button @click.native="btnSavemod" style="background-color:#e6e6e6;color:black">保存</el-button>
                                  </td>
                                  <td>
                                      <el-button @click.native="btnDelmod" style="background-color:#e6e6e6;color:black">删除</el-button>
                                  </td>
                              </tr>
                          </table>
                      </td>
                  </tr>
                  <tr>
                      <td align="right">打印顺序</td>
                      <td>
                          <el-select v-model="txtprintseq" style="width:100%">
                              <el-option v-for="(item,index) in printSeqOptions" :key="index" :value="item.value" :label="item.label"></el-option>
                          </el-select>
                      </td>
                      <td align="right">标本编号</td>
                      <td>
                          <el-input v-model="txtsampleno" placeholder="" style="width:100%" @keyup.enter.native="btnQuery" clearable></el-input>
                      </td>
                      <td colspan="4">
                          <p style="color:#CC6633">说明：标本号可以输入如：1,2,5-8等格式，为空表示全部</p>
                      </td>
                      <td align="center" colspan="2">
                          <el-checkbox true-label="1" false-label="0" v-model="txtnopatnameisprint">无病人姓名不打印</el-checkbox>
                      </td>
                  </tr>
              </table>
          </div>
          <div style="width:100%;float:left">
              <table style="float:right">
                  <tr>
                      <td>
                          <el-button type="primary" @click="btnQuery" icon="el-icon-search">查询</el-button>
                      </td>
                      <td>
                          <el-button @click="btnPrint" icon="el-icon-printer">打印</el-button>
                      </td>
                      <td>
                          <el-button @click="btnSaveDefault" icon="el-icon-document">存为默认</el-button>
                      </td>
                  </tr>
              </table>
          </div>
      </div>
      <div class="pagecenter">
          <div style="height:100%;width:100%;background-color:#ddccbb;">
              <vxe-table show-overflow ref="xTable1" height="auto" :loading="xTable1loading" :cell-class-name="cellClassName" :sort-config="{trigger: 'cell'}" :keyboard-config="keyConfig" :mouse-config="{selected: true}" :data.sync="LeftTableData" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe @current-change="currentChangeEvent" header-align="center">
                  <vxe-table-column field="emer_flag" title="加急" width="45" align="center" :formatter="formatterFun">
                  </vxe-table-column>
                  <vxe-table-column field="sampleno" title="标本编号" width="70" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="pat_name" title="姓名" width="60" sortable></vxe-table-column>
                  <vxe-table-column field="pat_sex" title="性别" width="50" align="center" :formatter="formatterFun"></vxe-table-column>
                  <vxe-table-column field="pat_ageyear" title="年龄" width="50"></vxe-table-column>
                  <vxe-table-column field="deptname" title="申请科室" width="100" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="wardname" title="病人病区" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="req_bedno" title="床号" width="60" align="center"></vxe-table-column>
                  <vxe-table-column field="cp_msg" title="打印提示" width="120" align="center"></vxe-table-column>
                  <vxe-table-column field="sampledate" title="标本日期" width="90" sortable :formatter="['toDateString', 'yyyy-MM-dd']"></vxe-table-column>
                  <vxe-table-column field="pat_no" title="病人号" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="pat_typecode" title="病人类别" width="80" :formatter="formatterFun"></vxe-table-column>
                  <vxe-table-column field="item_num" title="项目数" width="60" align="center"></vxe-table-column>
                  <vxe-table-column field="print_count" title="打印次数" width="80" align="center"></vxe-table-column>
                  <vxe-table-column field="reportid" title="报告ID" width="110"></vxe-table-column>
                  <vxe-table-column field="rptunitid" title="报告单元ID" width="90"></vxe-table-column>
                  <vxe-table-column field="warn_flag" title="警告" width="45" align="center">
                      <template v-slot="{row}">
                          <el-checkbox v-model="row.warn_flag" true-label="1" false-label="0" disabled></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="req_docno" title="申请医生" width="100"></vxe-table-column>

              </vxe-table>
          </div>
      </div>
      <!--病人来源多选-->
      <el-dialog title="病人来源多选" :visible.sync="multiPatsourceVisible" width="900px" v-el-drag-dialog :close-on-click-modal="false">
          <rm-SqlMultiple v-model="multiPatsource" :leftOption="leftOptionPat" :rightOption="rightOptionPat" @change="closedialog" name="pat"></rm-SqlMultiple>
      </el-dialog>
      <!--申请科室多选-->
      <el-dialog title="申请科室多选" :visible.sync="reqDeptVisible" width="900px" v-el-drag-dialog :close-on-click-modal="false">
          <rm-SqlMultiple v-model="reqDept" :leftOption="leftOptionDept" :rightOption="rightOptionDept" @change="closedialog" name="dept"></rm-SqlMultiple>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils";
export default {
  name: "LIS331",
  data() {
      return {
          leftOptionDept: [],
          rightOptionDept: [],
          leftOptionPat: [],
          rightOptionPat: [],
          modelOptons: [],
          multiPatsourceVisible: false,
          reqDeptVisible: false,
          multiPatsource: "", //病人来源多选值
          reqDept: "", //申请科室多选值
          txtrptunit: "",
          txtmodle: "",
          txtsampleno: "",
          txtdeptno: "",
          txtdeptgroup: "",
          txtrptDatetime: ["", ""],
          txtpattypecode: "",
          txtwardno: "",
          txtprintseq: "0",
          txtchkstate: "All",
          txtprintstat: "2",
          txthospcode: "",
          txtnopatnameisprint: "1",
          xTable1loading: false,
          xTable2loading: false,
          pattypeOption: [],
          printSeqOptions: [
              { label: "", value: "" },
              { label: "标本号", value: "0" },
              { label: "科室+床号+标本号", value: "1" },
              { label: "病人类型+科室+床号+标本号", value: "2" },
              { label: "病人类型+科室+标本号", value: "3" },
              { label: "科室+申请医生", value: "4" }
          ],
          chkOptions: [
              { label: "全部", value: "All" },
              { label: "未审核", value: "0" },
              { label: "已审核", value: "9" }
          ],
          printOptions: [
              { label: "全部", value: "All" },
              { label: "已打印", value: "1" },
              { label: "未打印", value: "2" }
          ],
          RightTableColumn: [],
          RightTableData: [],
          LeftTableData: [],
          tableColumn: [],
          sortArr: [],
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
              isArrow: true
          }
      };
  },
  created() {
      this.initOption();
      this.InitDate(); //初始化主列表
      this.InitPage(); //初始化查询
  },
  mounted() {
      this.modelOptons = [];
      let modOption = localStorage.getItem("rm_mod");
      let modOptions = JSON.parse(modOption);
      if (modOptions != null) {
          this.modelOptons = XEUtils.clone(modOptions, true);
      }
      let obj = localStorage.getItem("rm_default");
      if (obj != null) {
          let defaultList = JSON.parse(obj);
          this.setval(defaultList, "init");
      }
  },
  updated() {},
  methods: {
      deptnoChange(code, name) {
          this.txtdeptno = code;
      },
      wardnoChange(code, name) {
          this.txtwardno = code;
      },
      hospcodeChange(code, name) {
          this.txthospcode = code;
      },
      pattypeChange(code, name) {
          this.txtpattypecode = code;
      },
      deptgroupChange(code, name) {
          this.txtdeptgroup = code;
      },
      async initOption() {
          try {
              const res = await this.$tool.request({
                  url: "LIS/LIS3/LIS331",
                  method: "post",
                  data: {
                      action: "initOption"
                  }
              });

              this.pattypeOption = res.pattypeOption;
              this.leftOptionDept = res.deptOption;
              this.leftOptionPat = res.pattypeOption;
          } catch (e) {
              this.$message.warning(e);
          }
      },
      InitDate() {
          var day1 = new Date();
          day1.setTime(day1.getTime());
          var s1 = day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate() + " 23:59:59";
          var day2 = new Date();
          day2.setTime(day2.getTime());
          var s2 = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate() + " 00:00:00";
          this.txtrptDatetime = [s2, s1];
      },
      InitPage() {
          this.xTable1loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS331",
                  method: "post",
                  data: {
                      action: "InitPage",
                      txtrptunit: this.txtrptunit,
                      txtnopatnameisprint: this.txtnopatnameisprint,
                      txtprintstat: this.txtprintstat,
                      txtprintseq: this.txtprintseq,
                      txtchkstate: this.txtchkstate,
                      txtdeptgroup: this.txtdeptgroup,
                      txtstarttime: this.txtrptDatetime[0], //日期范围
                      txtendtime: this.txtrptDatetime[1], //日期范围
                      txthospcode: this.txthospcode,
                      txtpattypecode: this.txtpattypecode,
                      txtdeptno: this.txtdeptno,
                      txtwardno: this.txtwardno,
                      txtsampleno: this.txtsampleno
                  }
              })
              .then(response => {
                  this.LeftTableData = response.Data;
                  this.loadFirstRow(this.$refs.xTable1, "");
                  this.xTable1loading = false;
              })
              .catch(e => {
                  this.xTable1loading = false;
                  this.$message.warning(e);
              });
      },
      //自动加载表格的第一行，并执行回调函数
      loadFirstRow(xTable, func) {
          this.$nextTick(() => {
              // 加载第一行
              var selectrow = xTable.getData(0);
              if (selectrow) {
                  xTable.setCurrentRow(selectrow); //高亮行
                  xTable.scrollToRow(selectrow); //滚动到行
                  if (typeof func == "function") func(selectrow);
              } else {
                  xTable.setCurrentRow(null); //这个不会自动清空
              }
          });
      },
      formatterFun({ row, column, cellValue }) {
          if (column.property == "pat_sex") {
              if (cellValue == "1") {
                  return "男";
              } else if (cellValue == "2") {
                  return "女";
              } else {
                  return "";
              }
          } else if (column.property == "last_dt") {
              return XEUtils.toDateString(cellValue, "yyyy-MM-dd");
          } else if (column.property == "pat_typecode") {
              try {
                  let k = this.pattypeOption.find(m => m.data == cellValue).display;
                  if (k) {
                      return k;
                  } else {
                      return "";
                  }
              } catch (e) {}
          } else if (column.property == "emer_flag") {
              if (cellValue == "1") {
                  return "急";
              } else {
                  return "";
              }
          } else {
              return cellValue;
          }
      },
      currentChangeEvent({ row }) {},
      cellClassName({ row, column }) {
          if (column.property == "emer_flag") {
              if (row.emer_flag == "1") {
                  return "col-Green";
              }
          }
      },
      handlerrptitemchange(e) {
          this.txtrptunit = e;
      },
      btnQuery() {
          if(this.txtrptunit) {
              this.InitPage();
          } else {
              this.$alert("请选择报告单元！").then(() => {
                  this.$refs.rptunitinput.$refs.rptunitInput.focus();
              });
          }
      },
      //多选按钮
      async multiFun(type) {
          if (type == "pat") {
              await this.initOption();
              this.rightOptionPat = [];
              if (this.multiPatsource != "") {
                  let k = this.multiPatsource.split(",");
                  k.forEach(item => {
                      let ndata = this.leftOptionPat.find(m => m.data == item);
                      this.rightOptionPat.push(ndata);
                  });
              }
              this.multiPatsourceVisible = true;
          } else {
              await this.initOption();
              this.rightOptionDept = [];
              if (this.reqDept != "") {
                  let k = this.reqDept.split(",");
                  k.forEach(item => {
                      let ndata = this.leftOptionDept.find(m => m.data == item);
                      this.rightOptionDept.push(ndata);
                  });
              }
              this.reqDeptVisible = true;
          }
      },
      //多选弹窗关闭
      closedialog(value, name) {
          if (name == "pat") {
              this.multiPatsourceVisible = false;
              this.multiPatsource = value;
              this.txtpattypecode = value;
          } else {
              this.reqDeptVisible = false;
              this.reqDept = value;
              this.txtdeptno = value;
          }
      },
      async btnPrint() {
          if (typeof jsObj == "undefined") {
              this.$alert("请使用" + this.$pub.clientname + "打开系统", "错误", {
                  confirmButtonText: "确定"
              });
              return false;
          }
          let data = this.$refs.xTable1.afterFullData;
          if (data.length > 0) {
              const loading = this.$loading({
                  lock: true,
                  text: "打印中",
                  spinner: "el-icon-loading",
                  background: "rgba(0, 0, 0, 0.7)"
              });
              await this.stratPrint(0, data, data.length);
              // data.forEach(item => {});
              loading.close();
          } else {
              this.$alert("没有需要打印的数据", "提示", {
                  confirmButtonText: "确定"
              });
          }
      },
      async stratPrint(k, obj, len) {
          if (k < len) {
              try {
                  const res = await this.$tool.request({
                      url: "LIS/LIS3/LIS331",
                      method: "post",
                      data: {
                          action: "printreport",
                          reportid: obj[k].reportid,
                          userno: this.$tool.getSession(this.$tool.loginuserno),
                          printorview: "1"
                      }
                  });
                  if (res.Item1 == 1) {
                      this.$pub
                          .printreport(this, obj[k].reportid, "1", "", "1", "1")
                          .then(() => {
                              obj[k].cp_msg = "打印成功！";
                              this.stratPrint(++k, obj, len);
                          })
                          .catch((err) => {
                              // await this.stratPrint(k++,obj,len);
                              console.log("打印失败异常：printreport catch",err);
                              loading.close();
                              throw Error();
                          });
                  } else {
                      obj[k].cp_msg = res.Item1;
                      await this.stratPrint(++k, obj, len);
                  }
              } catch (e) {
                  console.log("打印出错！e",e);
                  this.$message("打印出错！" + e);
              }
          }
      },
      btnSaveDefault() {
          let modList = localStorage.getItem("rm_default");
          if (modList != null) {
              localStorage.removeItem("rm_default");
          }
          let tempjson = this.createJson();
          let modListJson = JSON.stringify(tempjson);
          localStorage.setItem("rm_default", modListJson);
      },
      createJson() {
          let jsondate = { txtrptunit: this.txtrptunit, txtprintseq: this.txtprintseq, txtpattypecode: this.txtpattypecode, txtdeptno: this.txtdeptno, txtwardno: this.txtwardno, txtdeptgroup: this.txtdeptgroup, txtchkstate: this.txtchkstate, txtprintstat: this.txtprintstat, txthospcode: this.txthospcode, txtmodle: this.txtmodle, txtsampleno: this.txtsampleno, txtnopatnameisprint: this.txtnopatnameisprint };
          return jsondate;
      },
      btnSavemod() {
          this.$prompt("查询模版保存名称", "文字编辑", {
              confirmButtonText: "确定",
              cancelButtonText: "取消"
              // inputPattern: /^\s*$/g,
              // inputErrorMessage: "模版名称格式不正确"
          })
              .then(({ value }) => {
                  if (value && value.trim() != "") {
                      this.modelOptons = [];
                      let modOption = localStorage.getItem("rm_mod");
                      let modOptions = JSON.parse(modOption);
                      let item = undefined;
                      if (modOptions != null) {
                          this.modelOptons = XEUtils.clone(modOptions, true);
                          item = modOptions.find(m => m.value == value);
                      }
                      if (item != undefined) {
                      } else {
                          this.modelOptons.push({ label: value, value: value });
                      }
                      let tempjson = this.createJson();
                      let modListJson = JSON.stringify(tempjson);
                      let modList = JSON.stringify(this.modelOptons);
                      localStorage.setItem("rm_mod_" + value, modListJson);
                      localStorage.setItem("rm_mod", modList);
                  }
              })
              .catch(() => {
                  this.$message({
                      type: "info",
                      message: "取消输入"
                  });
              });
      },
      ClearMod() {
          this.txtrptunit = " ";
          this.txtnopatnameisprint = " ";
          this.txtprintstat = "All";
          this.txtprintseq = "";
          this.txtchkstate = "All";
          this.txtdeptgroup = " ";
          this.txthospcode = " ";
          this.txtpattypecode = " ";
          this.txtdeptno = " ";
          this.txtwardno = " ";
          this.txtsampleno = "";
      },
      modChange(value) {
          let modList = localStorage.getItem("rm_mod");
          let tempOption = JSON.parse(modList);
          this.modelOptons = tempOption;
          let obj = localStorage.getItem("rm_mod_" + value);
          let jsondate = JSON.parse(obj);
          this.setval(jsondate, "change");
      },
      setval(jsondate, type) {
          this.$nextTick(() => {
              this.txtrptunit = jsondate.txtrptunit;
              this.txtnopatnameisprint = jsondate.txtnopatnameisprint;
              this.txtprintstat = jsondate.txtprintstat;
              this.txtprintseq = jsondate.txtprintseq;
              this.txtchkstate = jsondate.txtchkstate;
              this.txtdeptgroup = jsondate.txtdeptgroup;
              this.txthospcode = jsondate.txthospcode;
              this.txtpattypecode = jsondate.txtpattypecode;
              this.txtdeptno = jsondate.txtdeptno;
              this.txtwardno = jsondate.txtwardno;
              this.txtsampleno = jsondate.txtsampleno;
              if (type == "init") {
                  this.txtmodle = jsondate.txtmodle;
              }
          });
      },
      btnDelmod() {
          this.$confirm("您确定删除查询模版：" + this.txtstasticsmod + "吗？", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
          })
              .then(() => {
                  let value = this.txtmodle;
                  localStorage.removeItem("rm_mod_" + value);
                  let modList = localStorage.getItem("rm_mod");
                  let jsondate = JSON.parse(modList);
                  this.modelOptons = [];
                  jsondate.forEach(item => {
                      if (item.value == value) {
                      } else {
                          this.modelOptons.push(item);
                      }
                  });
                  let newmodList = JSON.stringify(this.modelOptons);
                  localStorage.setItem("rm_mod", newmodList);
                  this.txtmodle = "";
              })
              .catch(() => {});
      }
  }
};
</Script>

<style scoped>
.pagetop {
  width: 100%;
  height: 125px;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 130px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
/deep/ .vxe-table .vxe-body--column.col-red {
  background-color: #990033;
}
/deep/ .vxe-table .vxe-body--column.col-yellow {
  background-color: #999900;
}
/deep/ .vxe-table .vxe-body--column.col-green {
  background-color: #00ffcc;
}

/deep/.el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
/deep/.el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
/deep/ .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
/deep/ .vxe-table .vxe-body--column.col-Green {
  background-color: #00cc66;
}
</style>